from typing import List

from fastapi import APIRouter, HTTPException

from src.services.example.member_service import MemberService
from src.types.schema.example.member import MemberResp, MemberReq, MemberCreateReq, MemberUpdateReq
from src.utils.rs import Rs

router = APIRouter(prefix="/example")


@router.get("/member/all", response_model=Rs[List[MemberResp]])
async def member_all():
    data = await MemberService().all()
    return Rs().ok(data=data)


@router.post("/members", response_model=Rs[List[MemberResp]])
async def members(req: MemberReq):
    data = await MemberService().query(req)
    return Rs().ok(data=data)


@router.get("/member/{member_id}", response_model=Rs[MemberResp])
async def member_one(member_id: int):
    member = await MemberService().one(member_id)
    if not member:
        raise HTTPException(status_code=404, detail="User not found")
    return Rs().ok(member)


@router.post("/member/create", response_model=Rs[MemberResp])
async def create(req: MemberCreateReq):
    member = await MemberService().create(req)
    return Rs().ok(member)


@router.post("/member/update", response_model=Rs[MemberResp])
async def update(req: MemberUpdateReq):
    member = await MemberService().update(req)
    return Rs().ok(member)


@router.get("/member/delete/{member_id}", response_model=Rs)
async def delete(member_id: int):
    await MemberService().delete(member_id)
    return Rs().ok()
